package cn.edu.xmu.restfuldemo.controller;

import cn.edu.xmu.restfuldemo.model.BO.Orders;
import cn.edu.xmu.restfuldemo.model.VO.GoodsRetVo;
import cn.edu.xmu.restfuldemo.model.VO.OrdersRetVo;
import cn.edu.xmu.restfuldemo.model.VO.OrdersVo;
import cn.edu.xmu.restfuldemo.model.VoObject;
import cn.edu.xmu.restfuldemo.service.OrdersService;
import cn.edu.xmu.restfuldemo.service.RocketMQService;
import cn.edu.xmu.restfuldemo.util.Common;
import cn.edu.xmu.restfuldemo.util.ResponseCode;
import cn.edu.xmu.restfuldemo.util.ResponseUtil;
import cn.edu.xmu.restfuldemo.util.ReturnObject;
import lombok.Getter;
import org.apache.ibatis.ognl.ObjectElementsAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

/**
 * @author Team3-5
 */
@RestController
@RequestMapping(value = "/orders", produces = "application/json;charset=UTF-8")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @Autowired
    RocketMQService rocketMQService;

    @GetMapping("/{id}")
    public Object getOrdersById(@PathVariable("id") Long id) {
        ReturnObject returnObject =  ordersService.findOrders(id);
        return Common.getRetObject(returnObject);
    }

    @GetMapping("/{id}/other")
    public Object getOrdersByOrderId(@PathVariable("id") Long id) {
        ReturnObject returnObject =  ordersService.findOrdersRetVo(id);
        return Common.getRetObject(returnObject);
    }

    @PostMapping("")
    public Object addOrders(@RequestBody OrdersRetVo ordersRetVo){
        //System.out.println(ordersRetVo);
        ReturnObject returnObject = ordersService.addOrders(ordersRetVo);
        return Common.getRetObject(returnObject);
    }

    @PostMapping("/rocketmq")
    public Object addOrdersUsingRocketMq(@RequestBody OrdersRetVo ordersRetVo){
        rocketMQService.sendOrderMessage(ordersRetVo);
        return Common.getRetObject(new ReturnObject<>(ResponseCode.OK,"成功"));
    }

    @PostMapping("/test")
    public Object test(Long id){
        System.out.println(id);
        return id;
    }
}
